home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 85 / CD Temático 40 Febrero 2004.iso / DOS / ntfs / user / ntdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-15  |  4.9 KB  |  232 lines

  1. /*
  2.  *  ntdir.c
  3.  *
  4.  *  Copyright (C) 1995-1997 Martin von L÷wis
  5.  */
  6.  
  7. #ifdef HAVE_CONFIG_H
  8. #include "config.h"
  9. #endif
  10.  
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #ifdef HAVE_GETOPT_H
  14. #include <getopt.h>
  15. #else
  16. #define getopt_long(a,v,o,ol,x)        getopt(a,v,o)
  17. #endif
  18. #ifdef HAVE_UNISTD_H
  19. #include <unistd.h>
  20. #endif
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "ntfstypes.h"
  24. #include "struct.h"
  25. #include "util.h"
  26. #include "support.h"
  27. #include "nttools.h"
  28. #include "dir.h"
  29. #include "inode.h"
  30.  
  31. char *short_opts="lB:hUdnp1V";
  32. #ifdef HAVE_GETOPT_H
  33. struct option options[]={
  34.     {"long",0,0,'l'},
  35.     {"dos",0,0,'d'},
  36.     {"nt",0,0,'n'},
  37.     {"posix",0,0,'p'},
  38.     {"bias",1,0,'B'},
  39.     {"help",0,0,'h'},
  40.     {"unsorted",0,0,'U'},
  41.     {"8859-1",0,0,'1'},
  42.     {"version",0,0,'V'},
  43.     {0,0,0,0}
  44. };
  45. #endif
  46.  
  47. char usage_str[]=
  48. "ntdir: displays the contents of a NTFS directory\n"
  49. "ntdir [OPTIONS] path\n"
  50. "  --dos, -d                 Display only short names\n"
  51. "  --nt, -n                  Display only long names (default)\n"
  52. "  --posix, -p               Display all but hidden files\n"
  53. "  --long, -l                Display all files\n"
  54. "  --unsorted, -U            Do not sort names alphabetically\n"
  55. "  --8859-1, -1              Display names using ISO-8859-1\n"
  56. "                            (default is UTF-8)\n"
  57. "  --help, -h                Display this message\n"
  58. ;
  59.  
  60.  
  61. void usage(void)
  62. {
  63.     fprintf(stderr,usage_str);
  64. }
  65.  
  66. int ntfs_printcb(ntfs_u8 *entry,void *param)
  67. {
  68.     ntfs_volume *vol=(ntfs_volume*)param;
  69.     int flags=NTFS_GETU8(entry+0x51);
  70.     int show_hidden=0,to_lower=0;
  71.     char *name;
  72.     int name_len,i;
  73.     switch(vol->ngt){
  74.     case ngt_dos:
  75.         if((flags & 2) == 0)
  76.             return 0;
  77.         break;
  78.     case ngt_nt:
  79.         if(flags==2)
  80.             return 0;
  81.         if((flags & 2) == 2)
  82.             to_lower=1;
  83.         break;
  84.     case ngt_posix:
  85.         to_lower=1;
  86.         break;
  87.     case ngt_full:
  88.         show_hidden=1;
  89.         break;
  90.     }
  91.     if(!show_hidden && ((NTFS_GETU8(entry+0x48) & 2) == 2))
  92.         return 0;
  93.     /* last access time? */
  94.     print_time(NTFS_GETU64(entry+0x20));
  95.     /* file size and mft record number */
  96.     printf(" %10d %5d ",NTFS_GETU32(entry+0x40),NTFS_GETU32(entry));
  97.     if(ntfs_encodeuni(vol,(ntfs_u16*)(entry+0x52),NTFS_GETU8(entry+0x50),
  98.                &name,&name_len))
  99.     {
  100.         printf("(name print error)\n");
  101.         return 0;
  102.     }
  103.     if(to_lower)
  104.         for(i=0;i<name_len;i++)
  105.             putchar(tolower(name[i]));
  106.                 
  107.     else
  108.         printf("%s",name);
  109.     free(name);
  110.     putchar('\n');
  111.     return 0;
  112. }
  113.  
  114. /* display the directory ino */
  115. void ntfs_print_dir(ntfs_inode* ino)
  116. {
  117.     ntfs_u64 fileno;
  118.     int first;
  119.     char item[1000];
  120.     ntfs_iterate_s walk;
  121.     walk.type=BY_POSITION;
  122.     walk.dir=ino;
  123.     walk.u.pos=0;
  124.     walk.result=item;
  125.     for(fileno=0,first=1;1;)
  126.     {
  127.         /* get the next entry */
  128.         if(!ntfs_getdir_byposition(&walk))return;
  129.         /* on the first iteration, we won't get any data */
  130.         if(first){
  131.             first=0;
  132.             continue;
  133.         }
  134.         ntfs_printcb(item,ino->vol);
  135.     }
  136. }
  137.  
  138. void ntfs_print_dir_unsorted(ntfs_inode* ino)
  139. {
  140.     ntfs_u32 ph=0,pl=0;
  141.     int error;
  142.     while(1){
  143.         if((error=ntfs_getdir_unsorted(ino,&ph,&pl,
  144.                            ntfs_printcb,ino->vol)))
  145.             fprintf(stderr,"Dir error %x\n",error);
  146.         if(ph==0xFFFFFFFF)
  147.             return;
  148.     }
  149. }
  150.  
  151. /* find file name in directory ino, return MFT record number if found */
  152. int main(int argc,char *argv[])
  153. {
  154.     int c;
  155.     char device[256]={'\0'};
  156.     char *it;
  157.     char *name;
  158.     ntfs_volume *volume;
  159.     ntfs_inode ino;
  160.     int inum,bias=0;
  161.     unsigned int type=ngt_nt;
  162.     unsigned int charset=nct_utf8;
  163.     int unsorted=0;
  164.     extern int opterr,optind;
  165.     extern char* optarg;
  166.  
  167.     opterr=1;
  168.     while((c=getopt_long(argc,argv,short_opts,options,NULL))>0)
  169.         switch(c)
  170.         {
  171.         case 'd': type=ngt_dos;break;
  172.         case 'n': type=ngt_nt;break;
  173.         case 'p': type=ngt_posix;break;
  174.         case 'l': type=ngt_full;break;
  175.         case 'B': bias=strtol(optarg,NULL,0);break;
  176.         case 'h': usage();exit(0);break;
  177.         case 'U': unsorted=1;break;
  178.         case '1': charset=nct_iso8859_1;break;
  179.         case 'V': printf("ntdir " NTFS_VERSION "\n");exit(0);break;
  180.         default: usage();exit(1);
  181.         }
  182.     if(optind+1!=argc){
  183.         usage();
  184.         exit(1);
  185.     }
  186.     name=argv[optind];
  187.     for(it=name+2;*it && *it!='/';it++)
  188.         /*nothing*/;
  189.     if(it!=name+2)
  190.     {
  191.       strcpy(device,"/dev/");
  192.       strncpy(device+5,name+2,(it-name)-2);
  193.       device[(it-name)+3]='\0';
  194.     }
  195.     volume=ntfs_open_volume(device,bias,1,0);
  196.     if(!volume)return 1;
  197.     volume->ngt=type;
  198.     volume->nct=charset;
  199.     /* walk the directory */
  200.     inum=5;
  201.     name=it;
  202.     if(*name=='/')name++;
  203.     do{
  204.         char *next;
  205.         if(ntfs_init_inode(&ino,volume,inum))
  206.             fprintf(stderr,"error opening %s\n",name);
  207.         if(!name || !*name)break;
  208.         next=strpbrk(name,NTFS_PATH_SEP);
  209.         if(next){
  210.             *next='\0';
  211.             next++;
  212.         }
  213.         inum=ntfs_find_file(&ino,name);
  214.         if(inum==-1){
  215.             printf("%s not found\n",name);
  216.             return 1;
  217.         }
  218.         name=next;
  219.     }while(1);
  220.     if(unsorted)
  221.         ntfs_print_dir_unsorted(&ino);
  222.     else
  223.         ntfs_print_dir(&ino);
  224.     return 0;
  225. }
  226.  
  227. /*
  228.  * Local variables:
  229.  * c-file-style: "linux"
  230.  * End:
  231.  */
  232.